﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace TushuGuan_GUI
{
    public class Student
    {
        const int banben = 1;
        public string StudentID { get; set; }
        public List<BookBorrowed> Lend { get; set; } = new List<BookBorrowed>();

        public bool CanLend
        {
            get
            {
                if(Lend.Count>=limited)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        public int limited { get; set; } = 5;

        public void LendBook(string isbn,int timelimited)
        {
            if(Lend.Count>limited)
            {
                throw new Exception("借书数量超过上限：" + limited);
            }
            BookBorrowed bookBorrowed = new BookBorrowed
            {
                ISBN = isbn,
                Time = DateTime.Now,
                Lasttime=timelimited
            };
            Lend.Add(bookBorrowed);
        }

        public void ReturnBook(string isbn)
        {
            for(int i=0;i<Lend.Count;i++)
            {
                if(Lend[i].ISBN==isbn)
                {
                    Lend.RemoveAt(i);
                    return;
                }
            }
            throw new Exception("在该学生的借书单上找不到此书：" + isbn);
        }
        public string Save()
        {
            JObject data = new JObject(
                      new JProperty("banben", banben),
                      new JProperty("StudentID", StudentID),
                      new JProperty("limited", limited));

            data.Add(new JProperty("Count", Lend.Count));
            for (int i = 0; i < Lend.Count; i++)
            {
                data.Add(new JProperty(i.ToString(), Lend[i].Save()));
            }
            return data.ToString();
        }
        public void Load(string data)
        {
            var json = (JObject)JsonConvert.DeserializeObject(data);
            switch (int.Parse(json["banben"].ToString()))
            {
                case 1:
                    Load_1(data);
                    break;
                default:
                    return;
            }
        }
        private void Load_1(string data)
        {
            var json = (JObject)JsonConvert.DeserializeObject(data);
            var StudentID = json["StudentID"];
            var Count = json["Count"];
            var limited = json["limited"];

            this.limited = int.Parse(limited.ToString());
            this.StudentID = StudentID.ToString();
            int count_ = int.Parse(Count.ToString());

            Lend.Clear();
            for (int i = 0; i < count_; i++)
            {
                var linshi = json[i.ToString()];
                BookBorrowed book = new BookBorrowed();
                book.Load(linshi.ToString());

                Lend.Add(book);
            }

        }


    }
}
